import 'package:pub_semver/pub_semver.dart';
import 'package:safetymonitor/app/core/core.dart';
import 'package:safetymonitor/app/core/utils/log_util.dart';
import 'package:safetymonitor/app/data/entities/auth/app_upgrade_entity.dart';
import 'package:safetymonitor/global.dart';
import 'package:safetymonitor/modules/home/user/version_info/update_util.dart';

class VersionUpdateController extends GetxController {
  /// 升级信息实体
  AppUpgradeEntity? appUpgradeEntity;

  /// 是否有新版本
  bool hasNewVersion = false;

  @override
  void onInit() {
    super.onInit();
    // 获取版本信息
    checkUpdate();
  }

  /// 获取版本信息
  Future<void> checkUpdate() async {
    appUpgradeEntity = await UpdateUtil.getUpgradeInfo();
    try {
      hasNewVersion = Version.parse(appUpgradeEntity!.appVersion) > Version.parse(Global.packageInfo.version);
    } catch (e) {
      AppLogger.log('获取版本信息失败: $e');
    }
    update();
  }

  /// 点击升级
  void upgrade() {
    UpdateUtil.upgrade(appUpgradeEntity);
  }
}
